package a_o2odata_deal.utils

import a_aa_amainpackage.a_o2odata_deal.config.config.years
import com.alibaba.fastjson.JSON
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/4/8 10:33
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object modify_last {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName(s"${this.getClass.getSimpleName}")
    conf.set("spark.debug.maxToStringFields", "500")
    //conf.setMaster("local[*]")
    conf.set("es.nodes", "192.168.1.157")
    conf.set("es.port", "9200")
    conf.set("cluster.name", "O2OElastic")
    conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    conf.set("spark.sql.caseSensitive", "true")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    sc.setLogLevel("WARN")
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    sqlContext.read.json("s3a://o2o-dataproces-group/panzonghao/zhibo_zf/backups/all_userid/*/*")
      .dropDuplicates("userId").createOrReplaceTempView("user")

    import org.elasticsearch.spark._
    for (months <- 1 to 3){
      /*val tmallanchor: RDD[String] = sc.esJsonRDD(s"2020_tmall_anchor/tmall_anchor_2020_${months}").values
      sqlContext.read.json(tmallanchor).createOrReplaceTempView("tmallanchor")

      val taobaoanchor: RDD[String] = sc.esJsonRDD(s"2020_taobao_anchor/taobao_anchor_2020_${months}").values
      sqlContext.read.json(taobaoanchor).createOrReplaceTempView("taobaoanchor")

      val tmallgood: RDD[String] = sc.esJsonRDD(s"2020_tmall_good_live/tmall_good_live_2020_${months}").values
      sqlContext.read.json(tmallgood).createOrReplaceTempView("tmallgood")

      val taobaogood: RDD[String] = sc.esJsonRDD(s"2020_taobao_good_live/taobao_good_live_2020_${months}").values
      sqlContext.read.json(taobaogood).createOrReplaceTempView("taobaogood")*/

      sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/tmall/${years}/${months}/zhibo_zhubo").createOrReplaceTempView("tmallanchor")
      sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/taobao/${years}/${months}/zhibo_zhubo").createOrReplaceTempView("taobaoanchor")
      sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/tmall/${years}/${months}/zhibo_live_good").createOrReplaceTempView("tmallgood")
      sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/taobao/${years}/${months}/zhibo_live_good").createOrReplaceTempView("taobaogood")

      val tmallanchor_result = sqlContext.sql(
        s"""
           |select
           |*,
           |case when anchorId='69226163' then false else is_selfLives end as is_selfLivess
           |from
           |(select
           |t1.*,
           |case when t2.userId is null then false else true end as is_selfLives
           |from tmallanchor t1
           |left join
           |user t2
           |on t1.anchorId=t2.userId)
         """.stripMargin).drop("is_selfLive","is_selfLives")
        .withColumnRenamed("is_selfLivess","is_selfLive")

      val taobaoanchor_result = sqlContext.sql(
        s"""
           |select
           |*,
           |case when anchorId='69226163' then false else is_selfLives end as is_selfLivess
           |from
           |(select
           |t1.*,
           |case when t2.userId is null then false else true end as is_selfLives
           |from taobaoanchor t1
           |left join
           |user t2
           |on t1.anchorId=t2.userId)
         """.stripMargin).drop("is_selfLive","is_selfLives")
        .withColumnRenamed("is_selfLivess","is_selfLive")

     /* println(tmallanchor.count())
      println(tmallanchor_result.count())
      println(taobaoanchor.count())
      println(taobaoanchor_result.count())

      tmallanchor_result.where("nick='卫龙食品旗舰店'").show()
      taobaoanchor_result.where("nick='卫龙食品旗舰店'").show()*/

      val tmallgood_result = sqlContext.sql(
        s"""
           |select
           |*,
           |case when anchorId='69226163' then false else is_selfLives end as is_selfLivess
           |from
           |(select
           |t1.*,
           |case when t2.userId is null then false else true end as is_selfLives
           |from tmallgood t1
           |left join
           |user t2
           |on t1.anchorId=t2.userId)
         """.stripMargin).drop("is_selfLive","is_selfLives")
        .withColumnRenamed("is_selfLivess","is_selfLive")

      val taobaogood_result = sqlContext.sql(
        s"""
           |select
           |*,
           |case when anchorId='69226163' then false else is_selfLives end as is_selfLivess
           |from
           |(select
           |t1.*,
           |case when t2.userId is null then false else true end as is_selfLives
           |from taobaogood t1
           |left join
           |user t2
           |on t1.anchorId=t2.userId)
         """.stripMargin).drop("is_selfLive","is_selfLives")
        .withColumnRenamed("is_selfLivess","is_selfLive")

     /* println(tmallgood.count())
      println(tmallgood_result.count())
      println(taobaogood.count())
      println(taobaogood_result.count())

      tmallgood_result.where("nick='卫龙食品旗舰店'").show()
      taobaogood_result.where("nick='卫龙食品旗舰店'").show()*/

      tmallanchor_result.toJSON.rdd.map(line =>{
        JSON.parseObject(line)
      }).saveToEs(s"2020_tmall_anchor/tmall_anchor_2020_${months}", Map("es.mapping.id" -> "anchorId"))

      taobaoanchor_result.toJSON.rdd.map(line =>{
        JSON.parseObject(line)
      }).saveToEs(s"2020_taobao_anchor/taobao_anchor_2020_${months}", Map("es.mapping.id" -> "anchorId"))

      tmallgood_result.toJSON.rdd.map(line =>{
        JSON.parseObject(line)
      }).saveToEs(s"2020_tmall_good_live/tmall_good_live_2020_${months}", Map("es.mapping.id" -> "liveAndgood_id"))

      taobaogood_result.toJSON.rdd.map(line =>{
        JSON.parseObject(line)
      }).saveToEs(s"2020_taobao_good_live/taobao_good_live_2020_${months}", Map("es.mapping.id" -> "liveAndgood_id"))

    }




  }

}
